package com.pb.wkflow.api.controller;

import com.pb.infra.oth.entity.common.Result;
import com.pb.wkflow.api.controller.request.ProcessInstanceCreateRequest;
import com.pb.wkflow.api.controller.request.ProcessInstanceRequest;
import com.pb.wkflow.api.controller.request.ProcessTaskRequest;
import com.pb.wkflow.api.service.ProcInstService;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 流程实例API
 * @author 苗益辉
 * @date 2022/9/1 11:16
 */
@Slf4j
@RestController
@RequestMapping("v1/workflow/procInst")
public class ProcInstApi {

    /**
     * procInstService
     */
    @Autowired
    ProcInstService procInstService;

    /**
     * runtimeService
     */
    @Autowired
    RuntimeService runtimeService;

    /**
     * 开始流程
     * @param request request
     * @return Result
     */
    @PostMapping("start")
    public Result start(@RequestBody ProcessInstanceCreateRequest request) {
        log.info("---------- 开始流程 ---------- {}", request);
        return Result.ok(procInstService.startProcess(request, true)).setMessage("启动流程成功");
    }

    /**
     * 开始流程并提交第一个节点
     * @param request request
     * @return Result
     */
    @PostMapping("startAndSubmit")
    public Result startAndSubmit(@RequestBody ProcessInstanceCreateRequest request) {
        log.info("---------- 开始流程并提交第一个节点 ---------- {}", request);
        return Result.ok(procInstService.startAndSubmitProcess(request)).setMessage("启动流程成功");
    }

    /**
     * 终止流程
     * @param request request
     * @return Result
     */
    @PostMapping("terminateProcess")
    public Result terminateProcess(@RequestBody ProcessInstanceRequest request) {
        log.info("---------- 终止流程 ---------- {}", request);
        procInstService.terminateProcess(request);
        return Result.ok("终止流程成功");
    }

    /**
     * 删除流程实例
     * @param request request
     * @return Result
     */
    @PostMapping("deleteProcessInstance")
    public Result deleteProcessInstance(@RequestBody ProcessInstanceRequest request) {
        log.info("---------- 删除流程实例 ---------- {}", request);
        procInstService.deleteProcessInstance(request);
        return Result.ok("删除成功");
    }

    /**
     * 查询流程轨迹
     * @param request request
     * @return Result
     */
    @PostMapping("queryProcessTrack")
    public Result queryProcessTrack(@RequestBody ProcessInstanceRequest request) {
        log.info("---------- 查询流程轨迹 ---------- {}", request);
        return Result.ok(procInstService.queryProcessTrack(request)).setMessage("查询成功");
    }

    /**
     * 查询流程轨迹
     * @param request request
     * @return Result
     */
    @PostMapping("queryProcessTrack_v2")
    public Result queryProcessTrackV2(@RequestBody ProcessInstanceRequest request) {
        log.info("---------- 查询流程轨迹 ---------- {}", request);
        return Result.ok(procInstService.queryProcessTrackV2(request)).setMessage("查询成功");
    }

    /**
     * 查看详情
     * @param request request
     * @return Result
     */
    @PostMapping("getDetail")
    public Result getDetail(@RequestBody ProcessInstanceRequest request) {
        log.info("---------- 查询流程详情 ---------- {}", request);
        return Result.ok(procInstService.getDetail(request)).setMessage("查询成功");
    }


    /**
     * 获取当前节点处理人
     * @param request request
     * @return Result
     */
    @PostMapping("getParticipant")
    public Result getNextParticipant(@RequestBody ProcessInstanceRequest request) {
        log.info("---------- 获取当前节点处理人 ---------- {}", request);
        return Result.ok(procInstService.getParticipant(request));
    }

    /**
     * 查询流程进程
     * @param request request
     * @return Result
     */
    @PostMapping("queryInstance")
    public Result queryInstanceTodo(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                @RequestBody ProcessTaskRequest request) {
        log.info("---------- 查询流程进程 ---------- {}", request);
        return procInstService.queryInstance(pageIndex, pageSize, request);
    }
}
